using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Workflow2211A.API;
using Workflow2211A.Application;
using Workflow2211A.Application.Services;
using Workflow2211A.Application.Steps;
using Workflow2211A.Domain;
using WorkflowCore.Persistence.EntityFramework.Services;
using Yitter.IdGenerator;
//����һ��Ĭ�ϵ�Ӧ�ù��������Զ����������ļ����� appsettings.json�������������������в���
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
//���ӿ�����֧�֣����� MVC ģʽ���� HTTP ����
builder.Services.AddControllers();
//���� Swagger/OpenAPI �ĵ����ߣ��Զ����� API �ĵ���
//Ǳ�����⣺Swagger Ĭ������ XML ע�ͣ���������δ���� SwaggerGenOptions.IncludeXmlComments�����ܵ����ĵ�ȱʧ����������
builder.Services.AddSwaggerGen(x => 
{
    var path = AppDomain.CurrentDomain.BaseDirectory + "Workflow2211A.API.xml";
    var appPath = AppDomain.CurrentDomain.BaseDirectory + "Workflow2211A.Application.xml";
    x.IncludeXmlComments(path,true);
    x.IncludeXmlComments(appPath, true);
});
//ע��ִ�
builder.Services.AddScoped(typeof(IBaseRepository<>),typeof(BaseRepository<>));

//IOC�����Ʒ�ת)��DI������ע�룩
//IOC��һ��˼�룬DI��ʵ�����˼��ľ����ֶ�
//���Ʒ�ת�ĺô���������ù��ķ�����������ʲô���������������Ӧ��ȫ������IOC������
#region ��������ط���
/**
 * Transient:˲ʱ��ͬһ�������У������ע���Σ���ÿ�ζ��ǲ�ͬ��ʵ����
 * Scoped :  ������ͬһ�������У������ע���Σ���ÿ�ζ�����ͬ��ʵ����
 * Sigleton: �������������Ĵ����󣬶��ǹ���ͬһ��ʵ���������������ڶ��ǹ���ͬһ��ʵ��ֱ��������ֹͣ��
 */

//ע�����
builder.Services.AddTransient<IWorkflowService, WorkflowService>();
builder.Services.AddTransient<IUserService, UserService>();

builder.Services.AddScoped<StartStep>();
builder.Services.AddScoped<AuditStep>();
builder.Services.AddScoped<EndStep>();

builder.Services.AddDbContext<Workflow2211Context>();
//���ӹ�������ط��� ͨ�� AddWorkflow() ע�Ṥ��������֧�ֿ��ӻ�������ƺ�ִ�С�
builder.Services.AddWorkflow(x =>
{
    //�ڶ��������Ƿ��Զ��������ݿ⣬�������Ƿ��Զ�Ǩ�ƣ���ѡfalse
    x.UseMySQL(builder.Configuration.GetConnectionString("MySQL"), false, false);
});
//��jsonת��Ϊ���������ܹ���ʶ�Ĵ��루����ͨ�� JSON ���ù����������Ϳ����ż�����
builder.Services.AddWorkflowDSL();

//ע��AutoMapper
builder.Services.AddAutoMapper(x => 
{
    x.AddProfile<WorkflowProfile>();
});
//ע��ѩ��ID ��WorkerId����ID��
YitIdHelper.SetIdGenerator(new IdGeneratorOptions() { WorkerId =1 });

#endregion


// JWT Authentication 配置
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
var secretKeyString = jwtSettings["SecretKey"];
if (string.IsNullOrEmpty(secretKeyString))
{
    throw new InvalidOperationException("JWT SecretKey is not configured in appsettings.json");
}
var secretKey = Encoding.UTF8.GetBytes(secretKeyString);

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    options.RequireHttpsMetadata = false;
    options.SaveToken = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey(secretKey),
        ValidateIssuer = true,
        ValidIssuer = jwtSettings["Issuer"],
        ValidateAudience = true,
        ValidAudience = jwtSettings["Audience"],
        ValidateLifetime = true,
        ClockSkew = TimeSpan.Zero // Ĭ����5���ӣ���������Ϊ0
    };
});
builder.Services.AddAuthorization();
//
builder.Services.AddHttpContextAccessor();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())//�жϵ�ǰ��Ŀ�Ƿ��ǿ�������
//{
        //IsDevelopment ��������
        //IsEnvironment ���Ի���
        //IsProduction  ��������
        //IsStaging     Ԥ��������
//    app.UseSwagger();
//    app.UseSwaggerUI();
//}
//���� Swagger JSON �ĵ��˵㣨Ĭ�ϣ�/swagger/v1/swagger.json����
app.UseSwagger();
//���ÿ��ӻ� Swagger UI��Ĭ�ϣ�/swagger/index.html����
//app.UseSwaggerUI();

app.UseKnife4UI();

app.UseWorkflow();

app.UseAuthentication(); // ����֤
//���û��ڲ��Ե���Ȩ��֤������� [Authorize] ����ʹ�ã���
app.UseAuthorization();
//ӳ�������·�ɣ��� HTTP ����ַ�����Ӧ������������
app.MapControllers();

app.UseCors(x=>x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());

//������������ Kestrel ������������ HTTP ���󡣣�
app.Run();